var t = require("../../../@babel/runtime/helpers/objectSpread2"), e = require("../../../@babel/runtime/helpers/defineProperty");

require("../../../@babel/runtime/helpers/Objectvalues");

var o = getApp();

Page({
    data: {
        disabled_stepper: !1,
        query: {
            goodsId: 0,
            optionsId: "",
            orders: [],
            total: 1,
            memberCouponIds: [],
            note: ""
        },
        orderIds: 0,
        levelName: "",
        privacyDesc: "",
        goodsOrder: [],
        TemplateArray: [],
        clickMiniTemplate: !1,
        goodsDeductions: [],
        cup_notice: !0,
        coupon_size: 0,
        useCouponSize: 0,
        checkDeductionList: [],
        deduction_lang: "折扣",
        popupCouponSpecs: !1,
        agreementChecked: !1,
        orderShow: !1,
        totalPrice: 0,
        offlinePrice: "",
        name: "",
        mobile: "",
        pluginSetting: {},
        tagList: [],
        exchange_code_id: ""
    },
    onLoad: function(t) {
        var e;
        t.e_id && this.setData({
            exchange_code_id: t.e_id,
            disabled_stepper: !0
        }), t.total && this.setData({
            "query.total": t.total
        }), "" !== (null !== (e = t.goodsId) && void 0 !== e ? e : "") && (console.log(t.goodsId), 
        this.setData({
            "query.goodsId": parseInt(t.goodsId)
        }), this.orderPay(), this.preOrderData());
    },
    orderPay: function() {
        var t = this, e = o.getNetAddresss("order.goods-buy");
        this.data.e_id && (e = o.getNetAddresss("plugin.exchange-code.api.goods-buy"));
        var a = {
            goods_id: this.data.query.goodsId,
            option_id: this.data.query.optionsId,
            total: this.data.query.total,
            dispatch_type_id: 0,
            member_coupon_ids: this.data.memberCouponIds,
            orders: JSON.stringify(this._assembleDeduction())
        };
        this.data.exchange_code_id && (e = o.getNetAddresss("plugin.exchange-code.api.goods-buy"), 
        a.exchange_code_id = this.data.exchange_code_id), o._postNetWork({
            url: e,
            data: a,
            success: function(e) {
                var o = e.data;
                if (1 == o.result) {
                    var a, i, s;
                    console.log(o.data.orders);
                    var n, d = null === (a = o.data.orders[0]) || void 0 === a ? void 0 : a.order_deductions;
                    if ("[object Object]" === Object.prototype.toString.call(d)) d = Object.values(null === (n = o.data.orders[0]) || void 0 === n ? void 0 : n.order_deductions);
                    var r = [];
                    r.push({
                        pre_id: o.data.orders[0].pre_id
                    }), t._initCoupon(o.data.discount), t.setData({
                        goodsOrder: o.data.orders,
                        totalPrice: o.data.total_price,
                        goodsDeductions: d,
                        checkCouponList: null === (i = o.data.discount) || void 0 === i ? void 0 : i.member_coupons,
                        "query.note": null === (s = o.data.orders[0]) || void 0 === s ? void 0 : s.note,
                        "query.orders": r
                    });
                } else wx.showToast({
                    icon: "none",
                    title: o.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    preOrderData: function() {
        var t = this, e = o.getNetAddresss("plugin.coupon-store.frontend.order.preOrderData"), a = {
            goods_id: this.data.query.goodsId,
            basic_info: 1
        };
        o._postNetWork({
            url: e,
            data: a,
            success: function(e) {
                var o, a, i = e.data;
                1 == i.result ? t.setData({
                    levelName: i.data.level_name,
                    privacyDesc: i.data.privacy_desc,
                    offlinePrice: i.data.offline_price,
                    pluginSetting: null === (o = i.basic_info) || void 0 === o || null === (a = o.plugin_setting) || void 0 === a ? void 0 : a.coupon_store,
                    tagList: i.data.tag_list
                }) : wx.showToast({
                    icon: "none",
                    title: i.msg,
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getTemplate: function() {
        var t = o.getNetAddresss("member.member.getMiniTemplateCorrespond"), e = this;
        o._getNetWork({
            url: t,
            data: {
                small_type: 1,
                order_id: this.data.orderIds
            },
            success: function(t) {
                var o = t.data;
                if (1 == o.result) {
                    for (var a = [], i = 0; i < o.data.length; i++) a.push(o.data[i].template_id);
                    e.setData({
                        TemplateArray: a
                    }), e.data.TemplateArray.length > 0 ? wx.requestSubscribeMessage({
                        tmplIds: e.data.TemplateArray,
                        success: function(t) {
                            e.setData({
                                clickMiniTemplate: !0
                            }), wx.redirectTo({
                                url: "../../../packageD/buy/orderPay/orderPay?order_id=" + e.data.orderIds + "&status=2"
                            });
                        },
                        fail: function(t) {
                            console.log(t, "err"), wx.redirectTo({
                                url: "../../../packageD/buy/orderPay/orderPay?order_id=" + e.data.orderIds + "&status=2"
                            });
                        }
                    }) : wx.redirectTo({
                        url: "../../../packageD/buy/orderPay/orderPay?order_id=" + e.data.orderIds + "&status=2"
                    });
                } else Toast(o.msg);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    onOrderPay: function() {
        if (o._isTextEmpty(this.data.name)) wx.showToast({
            icon: "none",
            title: "请输入姓名",
            duration: 1500
        }); else if (o._isTextEmpty(this.data.mobile)) wx.showToast({
            icon: "none",
            title: "请输入手机号",
            duration: 1500
        }); else if (o._isTextEmpty(this.data.agreementChecked)) wx.showToast({
            icon: "none",
            title: "请同意个人信息保护声明",
            duration: 1500
        }); else {
            var t = o.getNetAddresss("order.create"), e = [], a = this;
            e.push({
                goods_id: this.data.query.goodsId,
                option_id: this.data.query.optionsId,
                total: this.data.query.total
            });
            var i = {
                dispatch_type_id: 0,
                name: this.data.name,
                mobile: this.data.mobile,
                member_coupon_ids: this.data.memberCouponIds,
                goods: JSON.stringify(e),
                orders: JSON.stringify(this._assembleDeduction())
            };
            this.data.exchange_code_id && (i.exchange_code_id = this.data.exchange_code_id, 
            t = o.getNetAddresss("plugin.coupon-store.frontend.create.index")), o._postNetWork({
                url: t,
                data: i,
                success: function(t) {
                    var e = t.data;
                    if (1 == e.result) return a.setData({
                        orderIds: e.data.order_ids
                    }), void (a.data.clickMiniTemplate ? (wx.showLoading({
                        title: "加载中"
                    }), wx.redirectTo({
                        url: "../../../packageD/buy/orderPay/orderPay?order_id=" + e.data.order_ids + "&status=2"
                    })) : a.getTemplate());
                    wx.showToast({
                        icon: "none",
                        title: e.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    onOrderSubmit: function() {
        this.onOrderPay();
    },
    onTotalChange: function(t) {
        this.setData({
            "query.total": t.detail
        }), this.orderPay();
    },
    onAgreementChange: function(t) {
        this.setData({
            agreementChecked: t.detail
        });
    },
    _initCoupon: function(t) {
        t.deduction_lang && this.setData({
            deduction_lang: t.deduction_lang
        }), this.setData({
            isShowCoupon: "1" == t.whether_show_coupon
        });
        var e = t.member_coupons;
        if (this.setData({
            is_coupon_SELE: "1" == t.coupon_show
        }), this.setData({
            coupon_size: e.length
        }), null != e && "" != e && null != e) if (this.data.is_coupon_SELE) {
            var o = e.sort(function(t, e) {
                return t.expired_at - e.expired_at;
            });
            this.initSelectCoupon(o);
        } else this.setData({
            coupons: e
        });
    },
    defaultSelectCoupon: function(t) {
        var e = [];
        t.forEach(function(t, o) {
            1 == t.checked && e.push(t);
        }), this.setData({
            coupon_size: t.length,
            coupons: t,
            useCouponSize: e.length,
            checkCouponList: e
        });
    },
    initSelectCoupon: function(t) {
        for (var e = t, o = [], a = {}, i = 0; i < e.length; i++) {
            var s = 0, n = 0, d = [ e[i].id ];
            e[i].checked && (console.log(e[i].checked), s++), e[i].valid && (console.log(e[i].valid), 
            n++);
            for (var r = i + 1; r < e.length; r++) e[i].coupon_id == e[r].coupon_id && (e[r].checked && s++, 
            e[r].valid && n++, d.push(e[r].id), e.splice(r, 1), r--);
            var u = e[i];
            u.has_conpon_id = d, u.valid_num = n, o.push(u), a[e[i].coupon_id] = {}, a[e[i].coupon_id].num = s;
        }
        this.setData({
            coupons_temp: a,
            coupons: o
        });
    },
    showCoupon: function() {
        0 != this.data.coupon_size ? this.setData({
            popupCouponSpecs: !0
        }) : wx.showToast({
            icon: "none",
            title: "暂无优惠券使用",
            duration: 1500
        });
    },
    selectCoupon: function(t) {
        var o = t.detail, a = t.currentTarget.dataset.index, i = t.currentTarget.dataset.item, s = [];
        for (var n in console.log(s), this.setData(e({}, "coupons[" + a + "].checked", o)), 
        this.data.coupons) this.data.coupons[n].checked && (console.log(this.data.coupons[n]), 
        s.push(this.data.coupons[n].id));
        this.setData({
            memberCouponIds: s,
            useCouponSize: s.length
        }), this._screenCoupon(o, i, t.type), this.orderPay();
    },
    _setCouponView: function() {
        this.setData({
            useCouponSize: this.data.checkCouponList.length
        });
    },
    _screenCoupon: function(t, e, o) {
        var a = e, i = this.data.checkCouponList;
        if (this.data.checkCouponList.length > 0) for (var s = 0; s < this.data.checkCouponList.length; s++) this.data.checkCouponList[s].id == a.id && i.splice(s, 1);
        this.data.is_coupon_SELE && o && (console.log(a.coupon_id), console.log(this.data.coupons_temp), 
        this.data.coupons_temp[a.coupon_id].num = t ? 1 : 0, this.setData({
            coupons_temp: this.data.coupons_temp
        })), t ? i.push(a) : this.setData({
            cup_notice: !0
        }), this.setData({
            checkCouponList: i
        }), console.log(i);
    },
    couponclose: function() {
        this.setData({
            popupCouponSpecs: !1
        });
    },
    mustSelectCoupon: function() {
        return !(!(this.data.openCueCoupon && this.data.coupon_size > 0 && this.data.useCouponSize < 1) || this.data.isCueCoupon) && (this.setData({
            isCueCoupon: !0,
            popupCouponSpecs: !0
        }), !0);
    },
    changeConpon: function(t) {
        var e = this, o = t.target.dataset.item, a = t.detail, i = t.target.dataset.index;
        if (a > o.valid_num) return wx.showToast({
            icon: "none",
            title: "使用张数已达上限",
            duration: 1e3
        }), clearTimeout(this.data.timer), void (this.data.timer = setTimeout(function() {
            e.data.coupons_temp[o.coupon_id].num = o.valid_num, e.setData({
                coupons_temp: e.data.coupons_temp
            });
        }, 200));
        if ("" != a && null != a && 0 != a) {
            clearTimeout(this.data.timer), this.data.coupons_temp[o.coupon_id].num = a, this.setData({
                coupons_temp: this.data.coupons_temp
            });
            var s = {
                detail: !0,
                currentTarget: {
                    dataset: {
                        index: i,
                        item: o
                    }
                }
            };
            this.data.timeoutId && clearTimeout(this.data.timeoutId), this.data.timeoutId = setTimeout(function() {
                e.selectCoupon(s);
            }, 1e3);
        } else wx.showToast({
            icon: "none",
            title: "暂不使用请取消勾选",
            duration: 1e3
        });
    },
    onLikeChange: function(t) {
        var e = this.data.goodsDeductions;
        e[t.currentTarget.dataset.index].checked = t.detail, this.setData({
            goodsDeductions: e
        }), this._assembleDeduction(), this.orderPay();
    },
    _assembleDeduction: function(e) {
        var o = [], a = [], i = this.data.goodsDeductions, s = {};
        return i.filter(function(t) {
            return !0 === t.checked;
        }).forEach(function(t) {
            a.push(t.code);
        }), this.data.query.orders.push({
            deduction_ids: a,
            note: this.data.query.note
        }), this.data.query.orders.forEach(function(e) {
            e && (s = t(t({}, s), e));
        }), Array.prototype.isPrototypeOf(this.data.query.orders) && 0 !== this.data.query.orders.length && o.push(s), 
        o;
    },
    modelInput: function(t) {
        var o = t.currentTarget.dataset.item, a = t.detail.value;
        this.setData(e({}, o, a));
    },
    onClose: function() {
        this.setData({
            orderShow: !1
        });
    },
    onAgreement: function() {
        this.setData({
            orderShow: !0
        });
    },
    onReady: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});